import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { nanoid } from 'nanoid'
import './index.css'

export default class Header extends Component {
  //对App组件传入的数据进行限制
  static propTypes = {
    addData: PropTypes.func.isRequired
  }

  //处理事件
  hander = (e) => {
    if (e.key === 'Enter') {
      if (e.target.value.trim() === '') {
        alert('输入不能为空')
        return
      }
      // console.log(e.target.value)
      const todoObj = { id: nanoid(), name: e.target.value, done: false }
      this.props.addData(todoObj)
      e.target.value = ''
    }
  }

  render() {
    return (
      <div className="todo-header">
        <input type="text" placeholder="请输入你的任务名称，按回车键确认" onKeyUp={this.hander} />
      </div>
    )
  }
}
